import qs from 'qs'
import {selectBindMarketingUser, marketBindStaff, marketDeleteStaff, marketAddStaff} from '@/api/marketing'

export default {
    data() {
        return {
            bindPage: {
                pageNum: 1,
                pageSize: 10,
                total: 0,
            },
            bingTableData: [],
            bindTableColumn: [
                {label: this.$t('员工姓名'), prop: 'adminName'},
                {label: this.$t('已绑定APP'), slot: 'bindApp', minWidth: 150},
                {label: this.$t('账号'), prop: 'accountNumber'},
                {label: this.$t('新建时间'), prop: 'createTime'},
                {label: this.$t('操作人'), prop: 'adminUserName'},
                {label: this.$t('操作'), slot: 'handle'},
            ],
            addVisible: false,
            addPage: {},
            addTableColumn: [
                {label: this.$t('员工姓名'), prop: 'adminName'},
                {label: this.$t('已绑定APP'), slot: 'bindApp'},
                {label: this.$t('账号'), prop: 'accountNumber'},
            ],
            addTableData: [],
            addSelectRows: []
        }
    },
    mounted() {
        this.getBindTableData()
    },
    methods: {
        async getBindTableData() {
            const res = await marketBindStaff({...this.bindPage, type: this.bindType})
            if (res.code === 1) {
                const {total, list} = res.data
                this.bingTableData = list
                this.bindPage = {
                    ...this.bindPage,
                    total,
                }
            }
        },
        bingSizeChange(val) {
            this.bindPage.pageSize = val
            this.getBindTableData()
        },
        bingPageChange(val) {
            this.bindPage.pageNum = val
            this.getBindTableData()
        },
        // 删除
        handleDelList(row) {
            this.$confirm(this.$t('解绑后,该员工将不再负责该类用户的营销工作'), this.$t('提示'), {
                type: 'warning'
            }).then(async () => {
                const res = await marketDeleteStaff({
                    id: row.id
                })
                if (res.code === 1) {
                    this.$message({
                        type: 'success',
                        message: this.$t('删除成功')
                    });
                    this.getBindTableData()
                }
            }).catch(() => {
            });
        },
        handleSelectionChange(value) {
            this.addSelectRows = value
        },
        clearSearch() {
            this.bindPage = {
                pageNum: 1,
                pageSize: 10,
                total: 0,
            }
            this.getBindTableData()
        },

        async addPersonnelTableData() {
            const res = await selectBindMarketingUser({
                ...this.addPage,
                type: this.bindType
            })
            if (res.code === 1) {
                this.addTableData = res.data
            }
        },
        // 新增员工提交
        async addConfirm() {
            if (this.addSelectRows.length === 0) return
            const selectRows = this.addSelectRows.map(i => {
                i.userId = i.adminUserId
                delete i.adminUserId
                return i
            })
            const res = await marketAddStaff({
                userMarketConfList: selectRows,
                type: this.bindType
            })
            if (res.code === 1) {
                this.addVisible = false
                this.addSelectRows = []
                this.$message.success(this.$t('新增成功'))
                this.getBindTableData()
            }
        }
    },
    beforeDestroy() {
        this.addVisible = false
    }

}
